#ifndef _OCTREE_H
#define _OCTREE_H

#include "Triangle.h"

#include <set>
using namespace std;

class Octree {
private:
	// The volume encompassed by this octree (and its childeren)
	AABB aabb; 
	// The children of this node
	Octree* child[8];
	// An internal node has child nodes
	bool internal;
	// Depth of the node (the length of the path to the root)
	int depth;
	// Triangles inside this node (only for leafs)
	set<Triangle*> triangle;
	// Total number of triangles this subtree contains
	int numTri;
	static const int maxOctreeDepth = 16;;
	static const int minTriPerOctree = 3;
	static const int maxTriPerOctree = 6;
public:
	Octree(const AABB& _aabb, int _depth);
	bool insert(Triangle* t);
	bool erase(Triangle* t);
	void update(Triangle* t);
	void handleCollisions();
	~Octree();
private:
	void merge();
	void split();
	bool erase(Triangle* t, const AABB& aabb);

};

#endif /* _OCTREE_H */
